from sqlalchemy import Column, String, Integer, DateTime, Text, JSON, Float, Boolean
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
import uuid

Base = declarative_base()

class Task(Base):
    """简化的任务模型"""
    __tablename__ = "tasks"

    id = Column(String(50), primary_key=True, default=lambda: str(uuid.uuid4()))
    name = Column(String(100), nullable=False)
    description = Column(Text)
    urls = Column(JSON, nullable=False)  # URL列表
    status = Column(String(20), default="pending")  # pending, running, completed, failed, stopped
    progress = Column(Float, default=0.0)
    pages_crawled = Column(Integer, default=0)
    pages_total = Column(Integer, default=0)
    success_rate = Column(Float, default=0.0)
    errors_count = Column(Integer, default=0)
    results = Column(JSON, nullable=True)  # 爬取结果JSON
    logs = Column(JSON, nullable=True)  # 日志JSON数组
    config = Column(JSON, nullable=True)  # 额外配置
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    started_at = Column(DateTime)
    completed_at = Column(DateTime)

class User(Base):
    """简化的用户模型"""
    __tablename__ = "users"

    id = Column(String(50), primary_key=True, default=lambda: str(uuid.uuid4()))
    username = Column(String(50), unique=True, nullable=False)
    email = Column(String(100), unique=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    full_name = Column(String(100))
    role = Column(String(20), default="user")  # admin, user
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    last_login = Column(DateTime)
